#!/usr/bin/env bash

# (C) Copyright 2020- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

ECTRANS_VERSION_STR="1.5.1"
ECTRANS_VERSION="1.5.1"
ECTRANS_GIT_SHA1=""

#################################################################
# Commands
#################################################################

usage()
{
  echo "Usage: ectrans [--version] [--info] [--git]"
  exit $1
}

version()
{
  echo "${ECTRANS_VERSION_STR}"
}

append_git()
{
  if (( $# > b )); then
    git="$@"
    echo ", git-sha1 ${git::${#git}-33}"
  fi
}

info()
{
  echo "ectrans version (${ECTRANS_VERSION_STR})$(append_git ${ECTRANS_GIT_SHA1})"
  echo ""
  echo "Build:"
  echo "  build type      : RelWithDebInfo"
  echo "  timestamp       : 20250420000820"
  echo "  op. system      : Linux-3.10.0-1160.90.1.el7.x86_64 (linux.64)"
  echo "  processor       : x86_64"
  echo "  c compiler      : GNU 12.2.0"
  echo "    flags         :  -pipe -O2 -g -DNDEBUG"
  echo "  fortran compiler: GNU 12.2.0"
  echo "    flags         :  -fallow-argument-mismatch -O2 -g -DNDEBUG"
  echo ""
  echo "Features:"
  echo "  MPI             : 1"
  echo "  OMP             : 1"
  echo "  MKL             : 0"
  echo "  FFTW            : "
  echo "  TRANSI          : 1"
  echo ""
  echo "Dependencies: "
  echo "  fiat version (1.5.0)$(append_git )"
  
}

gitsha1()
{
  echo "${ECTRANS_GIT_SHA1}"
}


#################################################################
# Parse command-line
#################################################################

if test $# -eq 0; then
    usage 1
fi

while test $# -gt 0; do

    # Split --option=value in $opt="--option" and $val="value"

    opt=""
    val=""

    case "$1" in
    --*=*)
      opt=`echo "$1" | sed 's/=.*//'`
      val=`echo "$1" | sed 's/--[_a-zA-Z0-9]*=//'`
      ;;
    --*)
      opt=$1
      ;;
    *)
      break
      ;;
    esac

    # Parse options
    case "$opt" in
      --version)
        version
        ;;
      --git)
        gitsha1
          ;;
      --info)
        info
        ;;
      --)
        shift
        break
        ;;
      *)
        echo "unknown option: $opt"
        usage 1
        ;;
    esac
    shift
done
